package cn.edu.sdjzu.xg.eagle.hairsalon.controller;

import com..entity.FuwuyuyueEntity;
import com.cn.edu.sdjzu.xg.eagle.hairsalon.entity.view.FuwuyuyueView;
import com.cn.edu.sdjzu.xg.eagle.hairsalon.service.FuwuyuyueService;
import com.cn.edu.sdjzu.xg.eagle.hairsalon.utils.MPUtil;
import com.cn.edu.sdjzu.xg.eagle.hairsalon.utils.PageUtils;
import com.cn.edu.sdjzu.xg.eagle.hairsalon.utils.R;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/fuwuyuyue")
public class FuwuyuyueController {
    @Autowired
    private FuwuyuyueService fuwuyuyueService;


    @RequestMapping("/page")
    public R page(@RequestParam Map<String, Object> params, FuwuyuyueEntity fuwuyuyue,
                  HttpServletRequest request) {
        String tableName = request.getSession().getAttribute("tableName").toString();
        if (tableName.equals("yuangong")) {
            fuwuyuyue.setYuangongzhanghao((String) request.getSession().getAttribute("username"));
        }
        if (tableName.equals("yonghu")) {
            fuwuyuyue.setZhanghao((String) request.getSession().getAttribute("username"));
        }
        EntityWrapper<FuwuyuyueEntity> ew = new EntityWrapper<>();
        PageUtils page = fuwuyuyueService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, fuwuyuyue), params), params));
        return R.ok().put("data", page);
    }


    @RequestMapping("/query")
    public R query(FuwuyuyueEntity fuwuyuyue) {
        EntityWrapper<FuwuyuyueEntity> ew = new EntityWrapper<>();
        ew.allEq(MPUtil.allEQMapPre(fuwuyuyue, MPUtil.camelToSnake("fuwuyuyue")));
        FuwuyuyueView fuwuyuyueView = fuwuyuyueService.selectView(ew);
        return R.ok("查询服务预约成功").put("data", fuwuyuyueView);
    }


    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        FuwuyuyueEntity fuwuyuyue = fuwuyuyueService.selectById(id);
        fuwuyuyue = fuwuyuyueService.selectView(new EntityWrapper<FuwuyuyueEntity>().eq("id", id));
        return R.ok().put("data", fuwuyuyue);
    }


    @RequestMapping("/save")
    public R save(@RequestBody FuwuyuyueEntity fuwuyuyue, HttpServletRequest request) {
        fuwuyuyueService.insert(fuwuyuyue);
        return R.ok();
    }


    @RequestMapping("/update")
    public R update(@RequestBody FuwuyuyueEntity fuwuyuyue, HttpServletRequest request) {
        fuwuyuyueService.updateById(fuwuyuyue);
        return R.ok();
    }


    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        fuwuyuyueService.deleteBatchIds(Arrays.asList(ids));
        return R.ok();
    }
}